import Vue from 'vue';
import iView from 'iview';
import VueRouter from 'vue-router';
import Routers from './router';
import Vuex from 'vuex';
import Util from './libs/util';
import App from './app.vue';
import 'iview/dist/styles/iview.css';
import './libs/normalize.css';
import Lodash from 'lodash';
import Rule from './libs/rule.js'
import custom from './views/components/custom.js'

import fhttp from './libs/fhttp.js'

/*import VueAwesomeSwiper from 'vue-awesome-swiper'
import 'swiper/dist/css/swiper.css'
Vue.use(VueAwesomeSwiper, /!* { default global options } *!/)*/
import './libs/publicstyle.less'
//  import BScroll from 'better-scroll' //弹性滚动插件
/* 转场动画插件 */
import vueg from 'vueg';
import 'vueg/css/transition-min.css';

import { Table, TableColumn } from 'element-ui'
Vue.use(Table);
Vue.use(TableColumn);
Vue.use(custom);

Vue.config.productionTip = false

Vue.use(VueRouter);
Vue.use(Vuex);

Vue.use(iView);

Vue.prototype._ = Lodash;
Vue.prototype.util = Util;
Vue.prototype.rule = Rule;
Vue.prototype.http = fhttp

// 路由配置
const RouterConfig = {
  routes: Routers
};
const router = new VueRouter(RouterConfig);

router.beforeEach((to, from, next) => {
    iView.LoadingBar.start();
    store.commit('setSpinShow', true);
    Util.title(to.meta.title);
    next();
    console.log(to)
    let path = to.path.split('/')
    if (path.length === 2) {
        store.commit('setBreadcrumb', [{name:'首页', path: ''}])
    } else if (path.length === 3) {
        store.commit('setBreadcrumb', [{name: '首页', path: '/sy', id: 'sy'}, {name: to.meta.title, paht: ''}])
    }/* else if (path.length === 4) {
     let title = to.meta.title.split('/')
     store.commit('setBreadcrumb', [{name: '首页', path: '/sy', id: 'sy'}, {name: title[0], path: '/' + path[1] + '/' + path[2], id: to.meta.id}, {name: title[1], path: ''}])
    }*/
});

router.afterEach(() => {
    iView.LoadingBar.finish();
    window.scrollTo(0, 0);
    store.commit('setSpinShow', false);
});

Vue.use(vueg, router);  //  转场动画

const store = new Vuex.Store({
    state: {
        currentTagName: null,
        breadcrumb: [],      //  面包屑导航
        tagdata: [],  //  tag标签
        spinShow: false,
        customer: [],
        user: [],
        product: [],
        materiel: [],
        order: [],
        work: [],
        role: [],
        uid: '',
        name: ''
    },
  getters: {

  },
  mutations: {
        setUid (state, uid) {
            state.uid = uid
        },
        setName (state, name) {
            state.name = name
        },
        setCurrentTagName (state, tagname) {
          state.currentTagName = tagname;
        },
        setBreadcrumb (state, breadcrumb) {
          state.breadcrumb = breadcrumb;
        },
        setTagdata (state, tagdata) {
          state.tagdata = tagdata;
        },
        addTagdata (state, tagdata) {
          state.tagdata.push(tagdata);
        },
        setSpinShow (state, spinshow) {
            state.spinShow = spinshow
        },
        updateTagData (state, item) {
            let istagdata = Lodash.findIndex(state.tagdata, (tagdata) => {
                return tagdata.id == item.id
            });
            Vue.set(state.tagdata, istagdata, item)
        },
        setCustomer (state, customer) {
            state.customer = customer
        },
        setUser (state, user) {
            state.user = user
        },
        setProduct (state, product) {
            state.product = product
        },
        setMateriel (state, materiel) {
            state.materiel = materiel
        },
        setOrder (state, order) {
            state.order = order
        },
        setWork (state, work) {
            state.work = work
        },
        setRole (state, role) {
            state.role = role
        }
  },
  actions: {

  }
});

new Vue({
  el: '#app',
  router,
  store,
  template: '<App/>',
  components: { App }
});
